/**
 * BlueprintController.java
 */
package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.yate.oa.model.Blueprint;
import org.yate.oa.model.FtpContent;
import org.yate.oa.result.AjaxResult;
import org.yate.oa.result.WebResult;
import org.yate.oa.service.BlueprintService;
import org.yate.oa.service.FtpContentService;
import org.yate.oa.utils.FileSuffixEnum;
import org.yate.oa.utils.FtpPathEnum;
import org.yate.oa.utils.Pagination;

import com.alibaba.fastjson.JSONObject;

/**
 * @author Yate
 * @date Feb 20, 2015
 * @description TODO
 * @version 1.0
 */
@Controller
@RequestMapping("/blueprint")
public class BlueprintController extends FtpController<FtpContent> {
	@Resource
	private BlueprintService service;

	@Resource
	private FtpContentService ftpService;

	@Resource
	private FtpController.IWrapperFactory<FtpContent> ftpFactory;

	@Override
	protected IWrapperFactory<FtpContent> getFactory() {
		return ftpFactory;
	}

	@RequestMapping("/page/list")
    private void pageList(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        PrintWriter out = resp.getWriter();
        JSONObject json = new JSONObject();
        String page = req.getParameter("page");
        int pageIndex = 1;
        if (!StringUtils.isEmpty(page)) {
            pageIndex = Integer.parseInt(page);
        }
        Pagination<Blueprint> p = this.service.getEntities(pageIndex, 10, new Blueprint(), null, null);
       json.put("status", "200");
       json.put("data", p);
       out.println(json);
       out.flush();
       out.close();
    }

	@RequestMapping("/page/add")
	public ModelAndView add(Blueprint bp, MultipartFile importLocalFile) {
		Map<String,Object> map = new HashMap<String, Object>();
			if(importLocalFile.getSize()>0){
				FtpContent f = null;
				try {
					// 校验文件后缀
					if (!this.validateFile(importLocalFile, FileSuffixEnum.PDF)) {
						// 文件后缀不对
					}
					// 上传文件
					f = ftpFactory.getProduce(importLocalFile, FtpPathEnum.BLUEPRINT.Path);
					  // 保存文件到ＮＦＳ硬盘
		            super.store(importLocalFile.getBytes(), f);
					ftpService.add(f);
					bp.setFtpId(f.getId());
				} catch (Exception e) {
					e.printStackTrace();
					// 文件上传失败
				}
				if (f == null) {
					// 文件上传失败
				}
			}
			service.add(bp);
			return WebResult.success("/tzgl/list", map);
	}
	
	@RequestMapping("/page/modify")
	public ModelAndView pageModify(Long id) {
		Map<String,Object> map = new HashMap<String, Object>();
		Blueprint t = this.service.getEntity(id);
		FtpContent f = ftpService.getEntity(t.getFtpId());
		t.setFtp(f);
		map.put("t", t);
		return WebResult.success("/tzgl/modify", map);
	}
	
	@RequestMapping("/remove")
	@ResponseBody
    public AjaxResult doDelete(Long id){
        service.remove(id);
        return AjaxResult.success();
    }
	
	@RequestMapping("/page/update")
	public String update(Blueprint bp,MultipartFile importLocalFile) {
		if(importLocalFile.getSize()>0){
			FtpContent f = null;
			try {
				// 校验文件后缀
				if (!this.validateFile(importLocalFile, FileSuffixEnum.PDF)) {
					// 文件后缀不对
				}

				// 上传文件
				f = ftpFactory.getProduce(importLocalFile, FtpPathEnum.BLUEPRINT.Path+importLocalFile.getOriginalFilename());
				  // 保存文件到ＮＦＳ硬盘
	            super.store(importLocalFile.getBytes(), f);
				ftpService.add(f);

			} catch (Exception e) {
				e.printStackTrace();
				// 文件上传失败
			}
			if (f == null) {
				// 文件上传失败
			}
			bp.setFtpId(f.getId());
		}
		service.update(bp);
		return "/tzgl/list";
	}

}
